package com.example.demo.controller;

import com.example.demo.core.dto.IdDto;
import com.example.demo.core.page.QueryCondition;
import com.example.demo.dto.UserSaveDto;
import com.example.demo.converter.UserConverter;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/save")
    public Object save(@RequestBody @Valid UserSaveDto dto) {
        return userService.save(UserConverter.INSTANCE.toUser(dto));
    }

    @PostMapping("/deleteById")
    public Object deleteById(@RequestBody @Valid IdDto dto) {
        userService.deleteById(dto.getId());
        return true;
    }

    @Secured("ADMIN")
    @PostMapping("/deleteAll")
    public Object deleteAll() {
        userService.deleteAll();
        return true;
    }

    @PostMapping("/findById")
    public Object findById(@RequestBody @Valid IdDto dto) {
        return userService.findById(dto.getId());
    }

    @Secured("USER")
    @PostMapping("/findAll")
    public Object findAll() {
        return userService.findAll();
    }

    @PostMapping("/findPage")
    private Object findPage(@RequestBody @Valid QueryCondition condition) {
        return userService.findPage(condition);
    }

}
